Common Errors
This page describes the errors you are most likely to encounter when getting started with Komplete Script, and explains what they mean and how to fix them.
Force-unwrapping a nil optional
What it looks like:
unwrapping a nil value
What it means:
You used the ! operator to force-unwrap an optional, but the value was nil at runtime.
var values = ["a": 0, "b": 1, "c": 2]
var fails = values["missing_key"]! // error — map lookup returns nil
How to fix it:
Check for nil before unwrapping, or use a ternary to supply a fallback:
var result = values["missing_key"] != nil ? values["missing_key"]! : -1
Or restructure to avoid the force-unwrap altogether:
if values["missing_key"] != nil {
print("Found: \{values["missing_key"]!}")
}
See Optionals for the full unwrapping reference.
Creating components inside a function
What it looks like:
Components are recreated repeatedly, causing flickering or unexpected resets of state.
What it means:
When you call a function inside a reactive expression (a property, computed state, or component body), Komplete Script tracks the function's arguments as dependencies. Every time those arguments change, the function is called again — and if the function creates components, new component instances are created and old ones are destroyed.
fun make_item(text: String) -> (Component) {
return Text(text) // new Text created every time text changes
}
How to fix it:
Use a template instead of a function when producing components. Templates forward their parameters lazily and do not create reactive dependencies on their arguments:
var make_item = template (text: String) {
Text(text) // stable — not recreated when text changes
}
See Templates for a detailed explanation of the difference.